package com.aiden.rxjavatest;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class NetWork {

    private OkHttpClient mClient;

    private long TIME_OUT = 10;

    private NetWork() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        mClient = builder.build();
    }

    public OkHttpClient getClient() {
        return mClient;
    }

    public static NetWork getInstance() {
        return SingletonHolder.netWork;
    }

    private static class SingletonHolder {
        private static NetWork netWork = new NetWork();
    }

    public void post(Map<String, String> params) {
        Request.Builder requestBuilder = new Request.Builder();
        if (params != null && params.size() > 0) {
            FormBody.Builder builder = new FormBody.Builder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                builder.add(entry.getKey(), String.valueOf(entry.getValue()));
            }
            requestBuilder.post(builder.build());
        }
        mClient.newCall(requestBuilder.build());

    }

}
